import { isNull, isNumber, isObject, isString, isArray } from "@/unit/tool";

const columns = [
  {
    title: "型号",
    dataIndex: "name",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "类型",
    dataIndex: "type",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "数量",
    dataIndex: "value",
    ellipsis: false,
    ellipsis: true
  },
];

function buildTableData(lists) {
  // let { afterSaleService, appointor, evaluation, repairList, suit } = res;
  let newLists = [];
  lists.forEach((item, index) => {
    newLists.push(buildListsData(item, index));
  });
  return newLists;
}

function buildListsData(item, index) {
  let newRes = { key: index };
  for (let key in item) {
    if (Object.hasOwnProperty.call(item, key)) {
      let obj = item[key];
      if (!isNull(obj)) {
        newRes = Object.assign(newRes, resolveParams(obj, key));
      }
    }
  }
  return newRes;
}

function resolveParams(val, key) {
  if (isNumber(val)) {
    return { [key]: val };
  } else if (isString(val)) {
    return { [key]: val };
  } else if (isArray(val)) {
    return { [key]: val };
  } else if (isObject(val)) {
    let { id, ...restVal } = val;
    return { ...restVal };
  }
}

export { columns, buildTableData };
